<?php

namespace App\Models;

use App\Utils\Output;
use Illuminate\Database\Eloquent\Model;
/**
 * @method static create(array $array) BaseModel
 */
class BaseModel extends Model
{
    use SoftDeletes;

    protected $dateFormat = 'U';


    /**
     * 删除
     * @param array|int $id
     * @return Output|int
     * @throws \Exception
     */
    static function destroy($id)
    {
        $entity = static::find($id);;
        if ($entity) {
            $entity->delete();
            return Output::success('操作成功');
        }
        return Output::fail("数据不存在");
    }

    /**
     * 存储
     * @param $data
     * @param int $id
     * @return Output
     */
    static function store($data, $id = 0)
    {
        if ($id) {
            $entity = static::find($id);
            if (empty($entity)) {
                return Output::fail('数据不存在');
            }
            $entity->fill($data);
        } else {
            $entity = static::create($data);
        }
        $entity->save();
        return Output::success('操作成功', $entity->id);
    }

    /**
     * 查询单个对象
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    static function find($id)
    {
        return static::query()->where('id', '=', $id)
            ->first();
    }
}
